import { getGetCodeByMail, getGetCodeByMobile, postAuthentication, queryCurrentUserInfo, getNotReadCount, getLogout } from '@/services/user';

export default {
  namespace: 'global',
  state: {
    information:{},
    count:0,
  },
  effects: {
    *getCodeByMail({ payload }, { call }) {
      const response = yield call(getGetCodeByMail, payload);
      const { code, msg } = response;
      if (code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },

    *getCodeByMobile({ payload }, { call }) {
      const response = yield call(getGetCodeByMobile, payload);
      const { code, msg } = response;
      if (code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
    *getUserInfo({ payload }, { call, put }) {
      const response = yield call(queryCurrentUserInfo);
      const { code, msg, data } = response;
      if (code === 1) {
        yield put({
          type: 'save',
          payload: data,
        });
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
    *fetchNotReadCount({ payload }, { call, put }) {
      const response = yield call(getNotReadCount);
      //console.log(response)
      const { code, data } = response
      if (code === 1) {
        yield put({
          type: 'saveCount',
          payload: data,
        });
      }
    },
    *logout({ payload }, { call }) {
      const response = yield call(getLogout);
      const { code, msg } = response
      //console.log(response)
      if (code === 1) {
        return Promise.resolve();
      }
      return Promise.reject(msg);
    },
  },

  *sendAuthentication({ payload }, { call, put }) {
    const response = yield call(postAuthentication, payload);
    const { code, msg } = response;
    if (code === 1) {
      return Promise.resolve(msg);
    }
    return Promise.reject(msg);
  },
  reducers: {
    save(state, { payload }) {
      state.information = payload;
      return state;
    },
    saveCount(state, { payload }) {
      state.count = payload
      return state
    },
  },
};
